﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Persistence;
using Kooboo.Extensions;
using System.Web;
using Kooboo.Web.Script.Serialization;
using Kooboo.CMS.Content.Models.Paths;
using System.IO;
using Kooboo.CMS.Content.EventBus;
using Kooboo.CMS.Content.EventBus.Content;

using Kooboo.Drawing.Filters;
using System.Drawing;
using System.Drawing.Imaging;
using Kooboo.Drawing;

namespace Kooboo.CMS.Toolkit.WatermarkContentEvent
{
    public class WatermarkSubscriber : ISubscriber
    {
        public void Receive(IEventContext context)
        {
            if (context is ContentEventContext && HttpContext.Current != null)
            {
                ContentEventContext contentEventContext = (ContentEventContext)context;
                var content = contentEventContext.Content;

                var result = (contentEventContext.ContentAction & ContentAction.PreAdd) == ContentAction.PreAdd || (contentEventContext.ContentAction & ContentAction.PreUpdate) == ContentAction.PreUpdate;

                if (result)
                {
                    if (content.ContentFiles != null)
                    {
                        content.ContentFiles = content.ContentFiles.ToArray();

                        foreach (var item in content.ContentFiles)
                        {
                            ImageFormat imageFormat;
                            if (item.Stream.Length > 0 && IsImageExtension(Path.GetExtension(item.FileName), out imageFormat))
                            {
                                item.Stream = Watermark(content.GetRepository(), item.Stream, imageFormat);
                            }
                        }
                    }
                }
            }
        }
        public static bool IsImageExtension(string extension, out ImageFormat imageFormat)
        {
            imageFormat = ImageFormat.Png;
            switch (extension.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    imageFormat = ImageFormat.Jpeg;
                    return true;
                case ".gif":
                    imageFormat = ImageFormat.Gif;
                    return true;
                case ".png":
                    imageFormat = ImageFormat.Png;
                    return true;
                case ".bmp":
                    imageFormat = ImageFormat.Bmp;
                    return true;
            }
            return false;
        }
        private Stream Watermark(Repository repository, Stream stream, ImageFormat imageFormat)
        {
            var waterImageFile = GetWaterImage(repository);
            if (File.Exists(waterImageFile))
            {
                Image waterImage = Image.FromFile(waterImageFile);

                Image rawImage = Image.FromStream(stream);

                ImageWatermarkFilter filter = new ImageWatermarkFilter();
                filter.WaterMarkImage = waterImage;
                filter.Alpha = 0.7f;
                filter.Valign = WaterMarkFilter.VAlign.Right;
                filter.Halign = WaterMarkFilter.HAlign.Bottom;
                var image = filter.ExecuteFilter(rawImage);

                MemoryStream ms = new MemoryStream();
                image.Save(ms, imageFormat);
                ms.Seek(0, SeekOrigin.Begin);
                return ms;
            }
            return stream;
        }
        private string GetWaterImage(Repository repository)
        {
            var path = new RepositoryPath(repository);

            return Path.Combine(path.PhysicalPath, "Watermark.png");
        }
    }
}
